#!/usr/bin/env python
# coding=utf-8


import os,sys
import re
import codecs
import glob
import shutil
import pyExcelerator as pE

SECTIONS = {
		'habitation': (
			r'#street name dictionary start',
			r'#district name dictionary end'
			),
		'num1':(
			r'#1 digit number dictionary start',
			r'#1 digit number dictionary end'
			),
		'num3':(
			r'#3 digit number dictionary start',
			r'#3 digit number dictionary end'
			),
		'num4':(
			r'#4 digit number dictionary start',
			r'#4 digit number dictionary end'
			),
		'num5':(
			r'#5 digit number dictionary start',
			r'#5 digit number dictionary end'
			),
		'landline':(
			r'#landline dictionary start',
			r'#landline dictionary end'
			),
		'mobile_phone_number':(
			r'#mobile phone number(13digit) dictionary start',
			r'#mobile phone number(13digit) dictionary end'
			),
		'landline1':(
			r'#landline1 dictionary start',
			r'#landline1 dictionary end'
			),
		'num34':(
			r'#3 or 4 digit number dictionary start',
			r'#3 or 4 digit number dictionary end'
			),
		'num345':(
			r'#3,4,5 Digit dictionary start',
			r'#3,4,5 Digit dictionary end'
			),
		'num789':(
			r'#7,8,9 Digit dictionary start',
			r'#7,8,9 Digit dictionary end'
			),
		'num101112':(
			r'#10,11,12 Digit dictionary start',
			r'#10,11,12 Digit dictionary end'
			)
}



LOG_FILE = open('ParseRes.log', 'w')
def LogPrint(s):
	print s
	print >> LOG_FILE, s


class CannotFindError(Exception): pass
def FindLino(lines, s):
	for n in range(len(lines)):
		if lines[n].find(s)>=0:
			return n
	for n in range(len(lines)):
		if re.search(s,lines[n]):
			return n
	raise CannotFindError()

def GetPercent(fnres):
	lines = codecs.open(fnres, encoding='utf-16').readlines()
	stLine = FindLino(lines, r'Cumulative percent of phrases matched by a phrase result number')
	m1 = re.match(r'^\s*1\s*\|\s*([\d\.]+)\s*$', lines[stLine+2])
	p1 = float(m1.groups()[0])
	return p1
#	m2 = re.match(r'^\s*2\s*\|\s*([\d\.]+)\s*$', lines[stLine+3])
#	p2 = float(m2.groups()[0])
#	m3 = re.match(r'^\s*3\s*\|\s*([\d\.]+)\s*$', lines[stLine+4])
#	p3 = float(m3.groups()[0])
#	return (p1,p2,p3)

def FillSheet(sheet,sn):
	dialects = ('Beijing', 'Gan', 'Kejia', 'Min','Wu', 'Xiang', 'Yue')
	for speaker in range(1,11):
		sheet.write(speaker+1, 0, 'speaker'+str(speaker))
	for col in range(1, 22):
		sheet.write(1, col, 'C'+str((col-1)%3+1))
	for n in range(len(dialects)):
		sheet.write(0, n*3+1, dialects[n])
		for speaker in range(1,11):
			fn1 = os.path.join('ResArchive',sn,dialects[n],'speaker'+str(speaker)+''+'.res')
			p1 = GetPercent(fn1)
			sheet.write(speaker+1, n*3+1, p1)
			fn2 = os.path.join('ResArchive',sn,dialects[n],'speaker'+str(speaker)+'_C2'+'.res')
			p2 = GetPercent(fn2)
			sheet.write(speaker+1, n*3+2, p2)
			fn3 = os.path.join('ResArchive',sn,dialects[n],'speaker'+str(speaker)+'_C3'+'.res')
			p3 = GetPercent(fn3)
			sheet.write(speaker+1, n*3+3, p3)
			
		
def CreateXls(xlsn):
	w = pE.Workbook()
	for sn in SECTIONS.keys():
		ws = w.add_sheet(sn)
		FillSheet(ws,sn)
	w.save(xlsn)

def __main__():
	CreateXls('ResArchive\\AllPercents.xls')
	print 'see ResArchive\\AllPercents.xls'


if __name__ == '__main__':
	__main__()

